package org.greatlogic.gwtfilereaderwidget.client.widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.DragEnterEvent;
import com.google.gwt.event.dom.client.DragLeaveEvent;
import com.google.gwt.event.dom.client.DragOverEvent;
import com.google.gwt.event.dom.client.DropEvent;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.event.shared.EventBus;
import org.vectomatic.dnd.DataTransferExt;
import org.vectomatic.dnd.DropPanel;
import org.vectomatic.file.File;
import org.vectomatic.file.FileList;
import org.vectomatic.file.FileUploadExt;

public class FileReaderPopupPanel extends PopupPanel {
//--------------------------------------------------------------------------------------------------
@UiField(provided = true)
static ResourceClientBundle bundle = GWT.create(ResourceClientBundle.class);
@UiField
DropPanel                   dropPanel;
@UiField
FileUploadExt               fileUpload;

private EventBus            _eventBus;
private File                _file;
//==================================================================================================
interface FileReaderPopupPanelCssResource extends CssResource {
public String dropPanel();
} // interface FileReaderPopupPanelCssResource
//==================================================================================================
interface IFileReaderPopupPanelBinder extends UiBinder<Widget, FileReaderPopupPanel> { //
} // interface IFileReaderPopupPanelBinder
//==================================================================================================
interface ResourceClientBundle extends ClientBundle {
@Source("FileReaderPopupPanel.css")
public FileReaderPopupPanelCssResource css();
} // interface ResourceClientBundle
//==================================================================================================
public static void requestFile(final String dropPanelText, final boolean multiple,
                               final EventBus eventBus) {
  FileReaderPopupPanel fileReaderPopupPanel = new FileReaderPopupPanel(dropPanelText, multiple,
                                                                       eventBus);
  fileReaderPopupPanel.showPanel();
} // requestFile()
//--------------------------------------------------------------------------------------------------
public FileReaderPopupPanel(final String dropPanelText, final boolean multiple,
                            final EventBus eventBus) {
  _eventBus = eventBus;
  bundle.css().ensureInjected();
  IFileReaderPopupPanelBinder binder = GWT.create(IFileReaderPopupPanelBinder.class);
  FlowPanel panel = (FlowPanel)binder.createAndBindUi(this);
  fileUpload.setMultiple(multiple);
  setWidget(panel);
  dropPanel.getElement().appendChild(Document.get().createDivElement()).appendChild(Document.get().createTextNode(dropPanelText));
} // FileReaderPopupPanel()
//--------------------------------------------------------------------------------------------------
@UiHandler("dropPanel")
public void dropPanelOnDragEnter(final DragEnterEvent event) {
  event.stopPropagation();
  event.preventDefault();
} // dropPanelOnDragEnter()
//--------------------------------------------------------------------------------------------------
@UiHandler("dropPanel")
public void dropPanelOnDragLeave(final DragLeaveEvent event) {
  event.stopPropagation();
  event.preventDefault();
} // dropPanelOnDragLeave()
//--------------------------------------------------------------------------------------------------
@UiHandler("dropPanel")
public void dropPanelOnDragOver(final DragOverEvent event) {
  event.stopPropagation();
  event.preventDefault();
} // dropPanelOnDragOver()
//--------------------------------------------------------------------------------------------------
@UiHandler("dropPanel")
public void dropPanelOnDrop(final DropEvent event) {
  FileList fileList = event.getDataTransfer().<DataTransferExt> cast().getFiles();
  event.stopPropagation();
  event.preventDefault();
  fireEvent(fileList);
} // dropPanelOnDrop()
//--------------------------------------------------------------------------------------------------
@UiHandler("fileUpload")
public void fileUploadOnChange(final ChangeEvent event) {
  FileList fileList = fileUpload.getFiles();
  fireEvent(fileList);
} // fileUploadOnChange()
//--------------------------------------------------------------------------------------------------
private void fireEvent(final FileList fileList) {
  hide();
  _eventBus.fireEvent(new FileReaderWidgetEvent(fileList));
} // fireEvent()
//--------------------------------------------------------------------------------------------------
public File getFile() {
  return _file;
} // getFile()
//--------------------------------------------------------------------------------------------------
private void showPanel() {
  setGlassEnabled(true);
  show();
  center();
} // showPanel()
//--------------------------------------------------------------------------------------------------
}